import grpc
from protos import user_pb2_grpc, user_pb2, address_pb2, address_pb2_grpc

def test_create_user(stub):
    try:
        request = user_pb2.CreateUserRequest()
        request.mobile = "123456789"
        response = stub.CreateUser(request)
        print(response.user.id, response.user.username)
        print(response)
        return response
    except grpc.RpcError as e:
        print(e.code())
        print(e.details())
    except Exception as e:
        print(e)

def test_get_user_by_id(stub):
    try:
        request = user_pb2.IdRequest()
        request.id = 1882326566411698176
        response = stub.GetUserById(request)
        print(response.user.id, response.user.username)
        print(response)
        return response
    except grpc.RpcError as e:
        print(e.code())
        print(e.details())
    except Exception as e:
        print(e)

def test_get_user_by_mobile(stub):
    try:
        request = user_pb2.MobileRequest()
        request.mobile = "1234562781111"
        response = stub.GetUserByMobile(request)
        print(response.user.id, response.user.username)
        print(response)
        return response
    except grpc.RpcError as e:
        print(e.code())
        print(e.details())
    except Exception as e:
        print(e)

def test_update_user_avatar(stub):
    try:
        request = user_pb2.AvatarRequest()
        request.id = 1887846104502894592
        request.avatar = "https://www.baidu.com"
        response = stub.UpdateAvatar(request)
        print(response.user.id, response.user.username)
        print(response)
        return response
    except grpc.RpcError as e:
        print(e.code())
        print(e.details())
    except Exception as e:
        print(e)

def test_update_user_username(stub):
    try:
        request = user_pb2.UsernameRequest()
        request.id = 1882326566411698176
        request.username = "test"
        response = stub.UpdateUsername(request)
        print(response.user.id, response.user.username)
        print(response)
        return response
    except grpc.RpcError as e:
        print(e.code())
        print(e.details())
    except Exception as e:
        print(e)


def test_update_user_password(stub):
    try:
        request = user_pb2.PasswordRequest()
        request.id = 1887846104502894592
        request.password = "123456"
        response = stub.UpdatePassword(request)
        print(response.user.id, response.user.username)
        print(response)
        return response
    except grpc.RpcError as e:
        print(e.code())
        print(e.details())
    except Exception as e:
        print(e)


def test_get_user_list(stub):
    try:
        request = user_pb2.PageRequest()
        request.page = 1
        request.size = 10
        response = stub.GetUserList(request)
        print(response)
        return response
    except grpc.RpcError as e:
        print(e.code())
        print(e.details())
    except Exception as e:
        print(e)

def test_varfiy_user(stub):
    try:
        request = user_pb2.VerifyUserRequest()
        request.mobile = '1234562781111'
        request.password = "123456"
        response = stub.VerifyUser(request)
        print(response)
        return response
    except grpc.RpcError as e:
        print(e.code())
        print(e.details())
    except Exception as e:
        print(e)

def test_get_or_create_user(stub):
    try:
        request = user_pb2.MobileRequest()
        request.mobile = '123456278222'
        response = stub.GetOrCreateUserByMobile(request)
        print(response)
        return response
    except grpc.RpcError as e:
        print(e.code())
        print(e.details())
    except Exception as e:
        print(e)

def test_create_address(stub):
    try:
        request = address_pb2.CreateAddressRequest()
        request.realname = '张三'
        request.mobile = '18899991111'
        request.region = '北京市房山区'
        request.detail = '阎村镇前沿村'
        request.user_id = 1891502286983659520
        response = stub.CreateAddress(request)
        print(response)
        return response
    except grpc.RpcError as e:
        print(e.code())
        print(e.details())
    except Exception as e:
        print(e)
def test_update_address(stub):
    try:
        request = address_pb2.UpdateAddressRequest()
        request.id = '0ced0ed812c74dbeb6dd75c8b82fdd86'
        request.realname = '张三'
        request.mobile = '18899990000'
        request.region = '北京市房山区11'
        request.detail = '阎村镇前沿村1'
        request.user_id = 1891502286983659520
        response = stub.UpdateAddress(request)
        print(response)
        return response
    except grpc.RpcError as e:
        print(e.code())
        print(e.details())
    except Exception as e:
        print(e)
def main():
    with grpc.insecure_channel('127.0.0.1:50051') as channel:
        userStub = user_pb2_grpc.UserStub(channel)
        # test_create_user(userStub)
        # test_get_user_by_id(userStub)
        # test_get_user_by_mobile(userStub)
        # test_update_user_avatar(userStub)
        # test_update_user_username(userStub)
        # test_get_user_list(userStub)
        # test_update_user_password(userStub)
        # test_varfiy_user(userStub)
        # test_get_or_create_user(userStub)

        addressStub = address_pb2_grpc.AddressStub(channel)
        # test_create_address(addressStub)
        test_update_address(addressStub)

if __name__ == '__main__':
    main()
